<!--
 * @Author: your name
 * @Date: 2021-07-10 16:19:41
 * @LastEditTime: 2021-07-10 16:38:49
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue-hr-project\src\views\employees\components\user-login-info.vue
-->
<template>
  <!-- 放置表单 -->
  <el-form
    ref="userForm"
    :model="userInfo"
    :rules="rules"
    label-width="120px"
    style="margin-left: 120px; margin-top:30px"
  >
    <el-form-item
      label="姓名:"
      prop="username"
    >
      <el-input
        v-model="userInfo.username"
        style="width:300px"
      />
    </el-form-item>
    <el-form-item
      label="密码:"
      prop="password"
    >
      <el-input
        v-model="userInfo.password"
        style="width:300px"
        type="password"
        disabled
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="uploadUserInfo">更新</el-button>
      <el-button @click="$router.back()">返回</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getUserDetailById, saveUserDetailById } from '@/api/employees'
export default {
  data() {
    return {
      userInfo: {
        username: '', // 用户名
        password: '' // 密码
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.loadUserInfo()
  },
  methods: {
    async loadUserInfo() {
      const res = await getUserDetailById(this.$route.query.id)
      //   console.log(res)
      this.userInfo = res.data
    },
    // 点击更新按钮
    uploadUserInfo() {
      // 兜底校验
      // 发送更新的接口请求
      this.$refs.userForm.validate(async(valid) => {
        if (valid) {
          // 全部校验通过
          // 发起请求 提示用户
          await saveUserDetailById(this.userInfo)
          this.$message({
            type: 'success',
            message: '更新信息成功'
          })
        }
      })
    }
  }
}
</script>
